package rjgc.mall.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import rjgc.mall.entity.Collection;
import rjgc.mall.entity.Goods;
import rjgc.mall.entity.ShoppingCart;
import rjgc.mall.entity.User;
import rjgc.mall.entity.vo.Collection2;
import rjgc.mall.entity.vo.CollectionList;
import rjgc.mall.entity.vo.ShoppingCarList;
import rjgc.mall.entity.vo.ShoppingCart2;
import rjgc.mall.mapper.CollectionMapper;
import rjgc.mall.mapper.GoodsMapper;
import rjgc.mall.mapper.TradeActionMapper;
import rjgc.mall.mapper.UserMapper;
import rjgc.mall.service.CollectionService;
import rjgc.mall.service.SellerService;
import rjgc.mall.service.ShoppingCartService;

import java.util.ArrayList;
import java.util.List;
@Service
public class CollectionServiceImpl implements CollectionService {
    @Autowired
    private CollectionMapper collectionMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private TradeActionMapper tradeActionMapper;
    @Autowired
    private UserMapper userMapper;


    @Override
    public int AddToCollection(Integer userid,Goods good) {
        Collection collection = new Collection();
        collection.setGoodid(good.getId());
        collection.setGoodname(good.getName());
        collection.setGoodprice(good.getPrice());
        collection.setUserid(userid);
        collection.setSelected(1);

        if (collectionMapper.userAndgoodExistOrNot(good.getId(),userid).isEmpty()){
            return collectionMapper.insertToDatabase(collection);
        }else{
            //System.out.println(shoppingCartMapper.userAndgoodExistOrNot(good.getId(),userid));
            return 2;//表示已经被收藏过
        }

    }

    @Override
    public int RemoveFromCollection(Integer id) {
        return collectionMapper.removeById(id);
    }

    @Override
    public int RemoveFromCollectionByStatus(Integer userid) {
        List<Collection> collectionList=collectionMapper.selectWhereSelectedEqualsOne(userid);
        for (int i=0;i<collectionList.size();i++)
        {
            int id = collectionList.get(i).getId();
            collectionMapper.removeById(id);
        }
        return 1;
    }

    @Override
    public List<Collection> ShowUserCollection(Integer userid) {
        return collectionMapper.showCollectionByUserid(userid);
    }

    @Override
    public int SelectAll(Integer userid) {
        return collectionMapper.selectAll(userid);
    }

    @Override
    public int NotSelectAll(Integer userid) {
        return collectionMapper.notSelectAll(userid);
    }

    @Override
    public int SelectOneOrNot(Integer id) {
        if (collectionMapper.showSelectStatus(id)==1){
            return collectionMapper.changeSelectedToZero(id);
        }
        else{
            return collectionMapper.changeSelectedToOne(id);
        }
    }

    @Override
    public CollectionList viewList(Integer userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        CollectionList result = new CollectionList();
        result.setUser(user);
        List<Collection> actions = collectionMapper.showCollectionByUserid(userId);
        List<Collection2> collections = new ArrayList<>();
        for(Collection action:actions){
            Collection2 colletction = new Collection2();
            Goods goods = goodsMapper.selectByPrimaryKey(action.getGoodid());
            colletction.setGoods(goods);
            collections.add(colletction);
        }
        result.setCollections(collections);
        return result;
    }

    @Override
    public Integer MoveToShoppingCart(Integer userid) {
        List<Collection> collectionList = collectionMapper.selectWhereSelectedEqualsOne(userid);
        for (int i=0;i<collectionList.size();i++){
            int goodid = collectionList.get(i).getGoodid();
            Goods good = goodsMapper.SelectGoodByGoodId(goodid);
            shoppingCartService.AddToShoppingCart(userid,good);
        }
        return 1;
    }

}
